package com.safety51.bi.controller;

import com.safety51.bi.entity.ApiResponse;
import com.safety51.bi.entity.ChartConfig;
import com.safety51.bi.service.ChartConfigService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/bi/config")
@CrossOrigin(origins = "*")
public class ChartConfigController {
    
    private final ChartConfigService chartConfigService;
    
    public ChartConfigController(ChartConfigService chartConfigService) {
        this.chartConfigService = chartConfigService;
    }
    
    @GetMapping("/all")
    public ApiResponse<List<ChartConfig>> getAllConfigs() {
        try {
            List<ChartConfig> configs = chartConfigService.getAllActiveConfigs();
            return ApiResponse.success(configs);
        } catch (Exception e) {
            return ApiResponse.error("获取配置失败: " + e.getMessage());
        }
    }
    
    @GetMapping("/{chartId}")
    public ApiResponse<ChartConfig> getConfig(@PathVariable String chartId) {
        try {
            ChartConfig config = chartConfigService.getConfigByChartId(chartId);
            if (config != null) {
                return ApiResponse.success(config);
            } else {
                return ApiResponse.error("配置不存在: " + chartId);
            }
        } catch (Exception e) {
            return ApiResponse.error("获取配置失败: " + e.getMessage());
        }
    }
    
    @PostMapping("/create")
    public ApiResponse<ChartConfig> createConfig(@RequestBody ChartConfig config) {
        try {
            // 这里应该实现保存逻辑
            // 简化处理，直接返回成功
            return ApiResponse.success(config);
        } catch (Exception e) {
            return ApiResponse.error("创建配置失败: " + e.getMessage());
        }
    }
}